/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;

import config.Models.Conf_Mix;
import config.Models.Conf_Mix.RequireMixEtt;
import config.Models.Conf_Mix_item;
import entities.Penguin;
import entities.User;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import libCore.Util;
import share.KeysDefinition;
import share.ShareConstants;
import utilities.time.UtilTime;

/**
 *
 * @author Mrkupi
 */
public class MixPenguinModel implements BaseModel {

    private String uId;
    
    public MixPenguinModel( String uid ) 
    {
        uId     =   uid;
    }
    
    @Override
    public void setData(Map<String, String> objStr) 
    {
    }

    @Override
    public void setDB() 
    {
    }

    @Override
    public void getDB(String uid) 
    {
    }
    
    public Map<String, Object> Mix( Map<String, Object> params ) 
    {
        Map<String, Object> data    =   new HashMap<String, Object>();
        String pengId   =   "";
        Boolean result  =   false;
        
        String coteId   =   String.valueOf( params.get( ShareConstants.COTE_ID ));
        
        // Get correctly formula from client
        String formulaId    =   String.valueOf( params.get( ShareConstants.FORMULA_ID ));
        Map<String, Conf_Mix> mConfMix  =   config.ConfigModels.conf_mixs;
        Map<String, Conf_Mix_item> mConfMixItem  =   config.ConfigModels.conf_mixItems;
        Conf_Mix    confMix =   mConfMix.get( formulaId );
        
        //=============== Check require gold/coin/level
        UserModel uModel    =   new UserModel( uId );
        User user   =   uModel.getDBUser();
        if( user.level < confMix.requireMix.level || user.coin < confMix.requireMix.coin || user.gold < confMix.requireMix.gold ) {
            // RETURN FAILED
            result  =   false;
        }
        
        //=============== Check penguin ======================
        // Get all penguin id from param
        // and get penguin from Redis
        //===========> Load penguin from id -> Client list
        int nPeng   =   Integer.parseInt( params.get( ShareConstants.NUMBER_PENGUIN ).toString() );
        String[] pList  =   new String[nPeng];
        List<Penguin> pengs     =   new ArrayList<Penguin>();
        for( int i = 0; i < nPeng; ++i ) {
            String iPengId      =   params.get( ShareConstants.PENG_ID + i ).toString();
            Penguin peng        =   U_PenguinModel.getPeg(iPengId);
            pengs.add( peng );
            pList[i]            =   iPengId;
        }
        
        List<RequireMixEtt> entities    =   confMix.requireMix.entities;
        boolean isCorrect   =   false;
        for( int i = 0; i < entities.size(); ++i )
        {
            // Counter type 
            RequireMixEtt ent           =   entities.get( i );
            Conf_Mix_item confMixItem   =   mConfMixItem.get( ent.mixItem );
            int count   =   0;
            for( int j = 0; j < pengs.size(); ++j )
            {
                if( pengs.get( j ).kind.equals( confMixItem.entity ) && pengs.get( j ).level >= confMixItem.level )
                    count++;
            }
            // Failed
            if( count != ent.sum )
            {
                // RETURN FAILED
                isCorrect   =   false;
                result      =   false;
            }
            isCorrect   =   true;
        }
        
        // Check formula complete with success
        if( isCorrect )
        {
            // Remove all penguin from this cote
            U_CoteModel coteModel   = new U_CoteModel( uId );
            boolean resultDel  =   coteModel.deletePenguins( coteId, pList );
            if( !resultDel )
            {
                // delete penguin fail
                result  =   false;
            }
            
            long time   =   UtilTime.getTimeNow();
            Penguin peng=   U_PenguinModel.createPeg( confMix.toMix.entity, 0, time );

            // Buy penguin and add to redis
            String pengRedis    =   Util.obj2String(peng);
            pengId      =   KeysDefinition.randomKeySTR();
            result      =   coteModel.addPenguin( coteId, pengId, pengRedis );
        }
        
        data.put( ShareConstants.CAN_MIX, result );
        data.put( ShareConstants.PENG_ID, pengId );
        
        return data;
    }
}
